D:\a\tools.proto\tools.proto\compiler\src\gen\rust\message\decl.rs
Line | Count | Source |
1 | | // Copyright (c) 2024, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use crate::compiler::message::Message; |
30 | | use crate::compiler::util::types::TypePathMap; |
31 | | use crate::gen::base::map::{DefaultTypeMapper, TypePathMapper}; |
32 | | use crate::gen::base::message::{gen_message_array_type_decls, generate, Templates}; |
33 | | use crate::gen::base::Error; |
34 | | use crate::gen::codec::CodecMap; |
35 | | use crate::gen::rust::util::RustUtils; |
36 | | use crate::gen::template::Template; |
37 | | use crate::gen::RustParams; |
38 | | |
39 | | const TEMPLATE: &[u8] = include_bytes!("decl.template"); |
40 | | const TEMPLATE_EXT: &[u8] = include_bytes!("ext.template"); |
41 | | |
42 | 28 | pub fn gen_message_decl( |
43 | 28 | msg: &Message, |
44 | 28 | codec_map: &CodecMap, |
45 | 28 | type_path_map: &TypePathMap, |
46 | 28 | params: &RustParams, |
47 | 28 | ) -> Result<String, Error> { |
48 | 28 | let type_path_map = TypePathMapper::new(type_path_map, DefaultTypeMapper); |
49 | 28 | let mut templates = Templates { |
50 | 28 | template: Template::compile(TEMPLATE_EXT).unwrap(), |
51 | 28 | codec_map, |
52 | 28 | }; |
53 | 28 | templates.template.var("msg_name", &msg.name); |
54 | 28 | let mut code = String::new(); |
55 | 28 | if params.enable_list_wrappers { Branch (55:8): [True: 6, False: 22]
Branch (55:8): [Folded - Ignored]
|
56 | 6 | code += &gen_message_array_type_decls::<RustUtils, _>(&templates, "wrappers", msg, &type_path_map)?0 ; |
57 | 22 | } |
58 | 28 | templates.template = Template::compile(TEMPLATE).unwrap(); |
59 | 28 | templates.template.var( |
60 | 28 | "generics", |
61 | 28 | RustUtils::get_generics(msg, &type_path_map).to_string_with_defaults(), |
62 | 28 | ); |
63 | 28 | code += &generate::<RustUtils, _>(templates, msg, &type_path_map)?0 ; |
64 | 28 | Ok(code) |
65 | 28 | } |